package net.xinqushi.java8stream;

import java.util.Arrays;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * @author xjie 2019/7/22 17:44
 * @description
 */
public class Concurrent {
    public static void main(String [] args) {
        int [] arr = {1,2,3,4,5,6,7,8,9,10};
        Spliterator<Integer> spliterator0 = Arrays.spliterator(arr);
        System.out.println(spliterator0.estimateSize());
        System.out.println(spliterator0.characteristics());
        System.out.println(spliterator0.getExactSizeIfKnown());
        System.out.println(spliterator0.hasCharacteristics(0));
        //System.out.println(spliterator0.getComparator().getClass());
//        spliterator0.tryAdvance((item )->{
//            System.out.println(item);
//        });
        spliterator0.tryAdvance(System.out::println);
        spliterator0.forEachRemaining((item )->{
            System.out.println(item);
        });
        Spliterator<Integer> spliterator1 = spliterator0.trySplit();
        Spliterator<Integer> spliterator2 = spliterator1.trySplit();
        Thread t0 = new SpliteratorThread<>(spliterator0);
        t0.setName("t0");
        Thread t1 = new SpliteratorThread<>(spliterator1);
        t1.setName("t1");
        Thread t2 = new SpliteratorThread<>(spliterator2);
        t2.setName("t2");
        t0.start();
        t1.start();
        t2.start();
    }

    static class SpliteratorThread<T> extends Thread {
        private Spliterator<T> mSpliterator;
        public SpliteratorThread(Spliterator<T> spliterator) {
            mSpliterator = spliterator;
        }

        @Override
        public void run() {
            super.run();
            if (mSpliterator != null) {
                mSpliterator.forEachRemaining(new Consumer<T>() {

                    @Override
                    public void accept(T t) {
                        System.out.println( Thread.currentThread().getName() + "-" + t + " ");
                    }
                });
            }
        }
    }
}
